package com.test.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockApp {

	public static void main(String[] args) {
		
		Lock lock = new ReentrantLock();
		Condition condition = lock.newCondition();
		for(int i=0;i<5;i++) {
			new Thread(()->{
				try {
					System.out.println("开始加锁");
					lock.lock();
					System.out.println("开始等待");
					condition.await();
					System.out.println("执行任务");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}finally {
					System.out.println("任务执行完毕释放锁资源");
					lock.unlock();
				}
			}) .start();
		}
		try {
			Thread.sleep(5000);
			lock.lock();
			System.out.println("主线程通知开始执行任务");
			condition.signalAll();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			System.out.println("主线程释放锁资源");
			lock.unlock();
		}
		

	}

}
